package com.xinqi.modules.sys.dict.manager.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.dict.domain.DictDocumentEntity;
import com.xinqi.modules.sys.dict.dto.req.DictDocumentQueryDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictDocumentResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictDocumentTreeResultDTO;
import com.xinqi.modules.sys.dict.manager.DictDocumentManager;
import com.xinqi.modules.sys.dict.mapper.DictDocumentMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文档分类字典表管理层实现
 *
 * @author: gzy
 * @date: 2023/05/28
 */
@Service
@RequiredArgsConstructor
public class DictDocumentManagerImpl extends BaseServiceImpl<DictDocumentMapper, DictDocumentEntity> implements DictDocumentManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DictDocumentResultDTO> findPage(Page<Object> page, DictDocumentQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    @Override
    public Page<DictDocumentResultDTO> findByPage(Page<Object> page, DictDocumentQueryDTO query) {
        return this.baseMapper.findByPage(page, query);
    }

    @Override
    public Long generateSort(long parentId) {
        Long sort = 0L;
        List<DictDocumentResultDTO> list = this.baseMapper.selectSubListByParentId(parentId);
        if (CollectionUtils.isNotEmpty(list)) {
            sort = Long.valueOf(list.size() + 1);
        } else {
            sort = 1L;
        }
        return sort;
    }

    @Override
    public Boolean removeOutById(Long id) {
        return this.baseMapper.deleteOutById(id) > 0;
    }

    @Override
    public String selectDictNameByCode(String dictCode) {
        return this.baseMapper.selectDictNameByCode(dictCode);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DictDocumentResultDTO> findList(DictDocumentQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 文档分类字典表数据
     */
    @Override
    public DictDocumentEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 文档分类字典表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<DictDocumentResultDTO> findAll() {
        return this.baseMapper.selectAll();
    }

    @Override
    public List<DictDocumentTreeResultDTO> findAllTree() {
        List<DictDocumentTreeResultDTO> baseList = this.baseMapper.selectAllTreeBase();
        return buildTreeUseStream(baseList, 0);
    }

    @Override
    public List<DictDocumentResultDTO> findSubListByParentId(Long parentId) {
        return this.baseMapper.selectSubListByParentId(parentId);
    }

    @Override
    public List<DictDocumentResultDTO> findSubListByParentCode(String parentCode) {
        return this.baseMapper.selectSubListByParentCode(parentCode);
    }

    @Override
    public int selectCountByCode(String dictCode) {
        return this.baseMapper.selectCountByCode(dictCode);
    }


    public static List<DictDocumentTreeResultDTO> buildTreeUseStream(List<DictDocumentTreeResultDTO> treeList, long id) {
        List<DictDocumentTreeResultDTO> list = treeList.stream()
            //过滤父节点与传递的id相同的TreeNode对象
            .filter(treeNode -> treeNode.getParentId().longValue() == id)
            .map(treeNode -> {
                //递归设置孩子节点
                treeNode.setChildren(buildTreeUseStream(treeList, treeNode.getId()));
                return treeNode;
            }).collect(Collectors.toList());
        return list;
    }
}
